home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / python2.6 / _abcoll.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2013-01-10  |  20.6 KB  |  713 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Abstract Base Classes (ABCs) for collections, according to PEP 3119.
  5.  
  6. DON'T USE THIS MODULE DIRECTLY!  The classes here should be imported
  7. via collections; they are defined here only to alleviate certain
  8. bootstrapping issues.  Unit tests are in test_collections.
  9. """
  10. from abc import ABCMeta, abstractmethod
  11. import sys
  12. __all__ = [
  13.     'Hashable',
  14.     'Iterable',
  15.     'Iterator',
  16.     'Sized',
  17.     'Container',
  18.     'Callable',
  19.     'Set',
  20.     'MutableSet',
  21.     'Mapping',
  22.     'MutableMapping',
  23.     'MappingView',
  24.     'KeysView',
  25.     'ItemsView',
  26.     'ValuesView',
  27.     'Sequence',
  28.     'MutableSequence']
  29.  
  30. def _hasattr(C, attr):
  31.     
  32.     try:
  33.         return (any,)((lambda .0: for B in .0:
  34. attr in B.__dict__)(C.__mro__))
  35.     except AttributeError:
  36.         return hasattr(C, attr)
  37.  
  38.  
  39.  
  40. class Hashable:
  41.     __metaclass__ = ABCMeta
  42.     
  43.     def __hash__(self):
  44.         return 0
  45.  
  46.     __hash__ = abstractmethod(__hash__)
  47.     
  48.     def __subclasshook__(cls, C):
  49.         if cls is Hashable:
  50.             
  51.             try:
  52.                 for B in C.__mro__:
  53.                     if '__hash__' in B.__dict__:
  54.                         if B.__dict__['__hash__']:
  55.                             return True
  56.                         break
  57.                         continue
  58.                     B.__dict__['__hash__']
  59.             except AttributeError:
  60.                 if getattr(C, '__hash__', None):
  61.                     return True
  62.             except:
  63.                 None<EXCEPTION MATCH>AttributeError
  64.             
  65.  
  66.         None<EXCEPTION MATCH>AttributeError
  67.         return NotImplemented
  68.  
  69.     __subclasshook__ = classmethod(__subclasshook__)
  70.  
  71.  
  72. class Iterable:
  73.     __metaclass__ = ABCMeta
  74.     
  75.     def __iter__(self):
  76.         while False:
  77.             yield None
  78.  
  79.     __iter__ = abstractmethod(__iter__)
  80.     
  81.     def __subclasshook__(cls, C):
  82.         if cls is Iterable:
  83.             if _hasattr(C, '__iter__'):
  84.                 return True
  85.         
  86.         return NotImplemented
  87.  
  88.     __subclasshook__ = classmethod(__subclasshook__)
  89.  
  90. Iterable.register(str)
  91.  
  92. class Iterator(Iterable):
  93.     
  94.     def next(self):
  95.         raise StopIteration
  96.  
  97.     next = abstractmethod(next)
  98.     
  99.     def __iter__(self):
  100.         return self
  101.  
  102.     
  103.     def __subclasshook__(cls, C):
  104.         if cls is Iterator:
  105.             if _hasattr(C, 'next'):
  106.                 return True
  107.         
  108.         return NotImplemented
  109.  
  110.     __subclasshook__ = classmethod(__subclasshook__)
  111.  
  112.  
  113. class Sized:
  114.     __metaclass__ = ABCMeta
  115.     
  116.     def __len__(self):
  117.         return 0
  118.  
  119.     __len__ = abstractmethod(__len__)
  120.     
  121.     def __subclasshook__(cls, C):
  122.         if cls is Sized:
  123.             if _hasattr(C, '__len__'):
  124.                 return True
  125.         
  126.         return NotImplemented
  127.  
  128.     __subclasshook__ = classmethod(__subclasshook__)
  129.  
  130.  
  131. class Container:
  132.     __metaclass__ = ABCMeta
  133.     
  134.     def __contains__(self, x):
  135.         return False
  136.  
  137.     __contains__ = abstractmethod(__contains__)
  138.     
  139.     def __subclasshook__(cls, C):
  140.         if cls is Container:
  141.             if _hasattr(C, '__contains__'):
  142.                 return True
  143.         
  144.         return NotImplemented
  145.  
  146.     __subclasshook__ = classmethod(__subclasshook__)
  147.  
  148.  
  149. class Callable:
  150.     __metaclass__ = ABCMeta
  151.     
  152.     def __call__(self, *args, **kwds):
  153.         return False
  154.  
  155.     __call__ = abstractmethod(__call__)
  156.     
  157.     def __subclasshook__(cls, C):
  158.         if cls is Callable:
  159.             if _hasattr(C, '__call__'):
  160.                 return True
  161.         
  162.         return NotImplemented
  163.  
  164.     __subclasshook__ = classmethod(__subclasshook__)
  165.  
  166.  
  167. class Set(Sized, Iterable, Container):
  168.     '''A set is a finite, iterable container.
  169.  
  170.     This class provides concrete generic implementations of all
  171.     methods except for __contains__, __iter__ and __len__.
  172.  
  173.     To override the comparisons (presumably for speed, as the
  174.     semantics are fixed), all you have to do is redefine __le__ and
  175.     then the other operations will automatically follow suit.
  176.     '''
  177.     
  178.     def __le__(self, other):
  179.         if not isinstance(other, Set):
  180.             return NotImplemented
  181.         if len(self) > len(other):
  182.             return False
  183.         for elem in self:
  184.             if elem not in other:
  185.                 return False
  186.         
  187.         return True
  188.  
  189.     
  190.     def __lt__(self, other):
  191.         if not isinstance(other, Set):
  192.             return NotImplemented
  193.         if len(self) < len(other):
  194.             pass
  195.         return self.__le__(other)
  196.  
  197.     
  198.     def __gt__(self, other):
  199.         if not isinstance(other, Set):
  200.             return NotImplemented
  201.         return other < self
  202.  
  203.     
  204.     def __ge__(self, other):
  205.         if not isinstance(other, Set):
  206.             return NotImplemented
  207.         return other <= self
  208.  
  209.     
  210.     def __eq__(self, other):
  211.         if not isinstance(other, Set):
  212.             return NotImplemented
  213.         if len(self) == len(other):
  214.             pass
  215.         return self.__le__(other)
  216.  
  217.     
  218.     def __ne__(self, other):
  219.         return not (self == other)
  220.  
  221.     
  222.     def _from_iterable(cls, it):
  223.         '''Construct an instance of the class from any iterable input.
  224.  
  225.         Must override this method if the class constructor signature
  226.         does not accept an iterable for an input.
  227.         '''
  228.         return cls(it)
  229.  
  230.     _from_iterable = classmethod(_from_iterable)
  231.     
  232.     def __and__(self, other):
  233.         if not isinstance(other, Iterable):
  234.             return NotImplemented
  235.         return (self._from_iterable,)((lambda .0: for value in .0:
  236. if value in self:
  237. valuecontinue)(other))
  238.  
  239.     
  240.     def isdisjoint(self, other):
  241.         for value in other:
  242.             if value in self:
  243.                 return False
  244.         
  245.         return True
  246.  
  247.     
  248.     def __or__(self, other):
  249.         if not isinstance(other, Iterable):
  250.             return NotImplemented
  251.         chain = (lambda .0: for s in .0:
  252. for e in s:
  253. e)((self, other))
  254.         return self._from_iterable(chain)
  255.  
  256.     
  257.     def __sub__(self, other):
  258.         if not isinstance(other, Set):
  259.             if not isinstance(other, Iterable):
  260.                 return NotImplemented
  261.             other = self._from_iterable(other)
  262.         
  263.         return (self._from_iterable,)((lambda .0: for value in .0:
  264. if value not in other:
  265. valuecontinue)(self))
  266.  
  267.     
  268.     def __xor__(self, other):
  269.         if not isinstance(other, Set):
  270.             if not isinstance(other, Iterable):
  271.                 return NotImplemented
  272.             other = self._from_iterable(other)
  273.         
  274.         return self - other | other - self
  275.  
  276.     __hash__ = None
  277.     
  278.     def _hash(self):
  279.         """Compute the hash value of a set.
  280.  
  281.         Note that we don't define __hash__: not all sets are hashable.
  282.         But if you define a hashable set type, its __hash__ should
  283.         call this function.
  284.  
  285.         This must be compatible __eq__.
  286.  
  287.         All sets ought to compare equal if they contain the same
  288.         elements, regardless of how they are implemented, and
  289.         regardless of the order of the elements; so there's not much
  290.         freedom for __eq__ or __hash__.  We match the algorithm used
  291.         by the built-in frozenset type.
  292.         """
  293.         MAX = sys.maxint
  294.         MASK = 2 * MAX + 1
  295.         n = len(self)
  296.         h = 1927868237 * (n + 1)
  297.         h &= MASK
  298.         for x in self:
  299.             hx = hash(x)
  300.             h ^= (hx ^ hx << 16 ^ 89869747) * 0xD93F34D7L
  301.             h &= MASK
  302.         
  303.         h = h * 69069 + 907133923
  304.         h &= MASK
  305.         if h > MAX:
  306.             h -= MASK + 1
  307.         
  308.         if h == -1:
  309.             h = 590923713
  310.         
  311.         return h
  312.  
  313.  
  314. Set.register(frozenset)
  315.  
  316. class MutableSet(Set):
  317.     
  318.     def add(self, value):
  319.         '''Add an element.'''
  320.         raise NotImplementedError
  321.  
  322.     add = abstractmethod(add)
  323.     
  324.     def discard(self, value):
  325.         '''Remove an element.  Do not raise an exception if absent.'''
  326.         raise NotImplementedError
  327.  
  328.     discard = abstractmethod(discard)
  329.     
  330.     def remove(self, value):
  331.         '''Remove an element. If not a member, raise a KeyError.'''
  332.         if value not in self:
  333.             raise KeyError(value)
  334.         value not in self
  335.         self.discard(value)
  336.  
  337.     
  338.     def pop(self):
  339.         '''Return the popped value.  Raise KeyError if empty.'''
  340.         it = iter(self)
  341.         
  342.         try:
  343.             value = next(it)
  344.         except StopIteration:
  345.             raise KeyError
  346.  
  347.         self.discard(value)
  348.         return value
  349.  
  350.     
  351.     def clear(self):
  352.         '''This is slow (creates N new iterators!) but effective.'''
  353.         
  354.         try:
  355.             while True:
  356.                 self.pop()
  357.         except KeyError:
  358.             pass
  359.  
  360.  
  361.     
  362.     def __ior__(self, it):
  363.         for value in it:
  364.             self.add(value)
  365.         
  366.         return self
  367.  
  368.     
  369.     def __iand__(self, it):
  370.         for value in self - it:
  371.             self.discard(value)
  372.         
  373.         return self
  374.  
  375.     
  376.     def __ixor__(self, it):
  377.         if not isinstance(it, Set):
  378.             it = self._from_iterable(it)
  379.         
  380.         for value in it:
  381.             if value in self:
  382.                 self.discard(value)
  383.                 continue
  384.             self.add(value)
  385.         
  386.         return self
  387.  
  388.     
  389.     def __isub__(self, it):
  390.         for value in it:
  391.             self.discard(value)
  392.         
  393.         return self
  394.  
  395.  
  396. MutableSet.register(set)
  397.  
  398. class Mapping(Sized, Iterable, Container):
  399.     
  400.     def __getitem__(self, key):
  401.         raise KeyError
  402.  
  403.     __getitem__ = abstractmethod(__getitem__)
  404.     
  405.     def get(self, key, default = None):
  406.         
  407.         try:
  408.             return self[key]
  409.         except KeyError:
  410.             return default
  411.  
  412.  
  413.     
  414.     def __contains__(self, key):
  415.         
  416.         try:
  417.             self[key]
  418.         except KeyError:
  419.             return False
  420.  
  421.         return True
  422.  
  423.     
  424.     def iterkeys(self):
  425.         return iter(self)
  426.  
  427.     
  428.     def itervalues(self):
  429.         for key in self:
  430.             yield self[key]
  431.         
  432.  
  433.     
  434.     def iteritems(self):
  435.         for key in self:
  436.             yield (key, self[key])
  437.         
  438.  
  439.     
  440.     def keys(self):
  441.         return list(self)
  442.  
  443.     
  444.     def items(self):
  445.         return [ (key, self[key]) for key in self ]
  446.  
  447.     
  448.     def values(self):
  449.         return [ self[key] for key in self ]
  450.  
  451.     __hash__ = None
  452.     
  453.     def __eq__(self, other):
  454.         if not isinstance(other, Mapping):
  455.             return NotImplemented
  456.         return dict(self.items()) == dict(other.items())
  457.  
  458.     
  459.     def __ne__(self, other):
  460.         return not (self == other)
  461.  
  462.  
  463.  
  464. class MappingView(Sized):
  465.     
  466.     def __init__(self, mapping):
  467.         self._mapping = mapping
  468.  
  469.     
  470.     def __len__(self):
  471.         return len(self._mapping)
  472.  
  473.  
  474.  
  475. class KeysView(MappingView, Set):
  476.     
  477.     def __contains__(self, key):
  478.         return key in self._mapping
  479.  
  480.     
  481.     def __iter__(self):
  482.         for key in self._mapping:
  483.             yield key
  484.         
  485.  
  486.  
  487.  
  488. class ItemsView(MappingView, Set):
  489.     
  490.     def __contains__(self, item):
  491.         (key, value) = item
  492.         
  493.         try:
  494.             v = self._mapping[key]
  495.         except KeyError:
  496.             return False
  497.  
  498.         return v == value
  499.  
  500.     
  501.     def __iter__(self):
  502.         for key in self._mapping:
  503.             yield (key, self._mapping[key])
  504.         
  505.  
  506.  
  507.  
  508. class ValuesView(MappingView):
  509.     
  510.     def __contains__(self, value):
  511.         for key in self._mapping:
  512.             if value == self._mapping[key]:
  513.                 return True
  514.         
  515.         return False
  516.  
  517.     
  518.     def __iter__(self):
  519.         for key in self._mapping:
  520.             yield self._mapping[key]
  521.         
  522.  
  523.  
  524.  
  525. class MutableMapping(Mapping):
  526.     
  527.     def __setitem__(self, key, value):
  528.         raise KeyError
  529.  
  530.     __setitem__ = abstractmethod(__setitem__)
  531.     
  532.     def __delitem__(self, key):
  533.         raise KeyError
  534.  
  535.     __delitem__ = abstractmethod(__delitem__)
  536.     __marker = object()
  537.     
  538.     def pop(self, key, default = __marker):
  539.         
  540.         try:
  541.             value = self[key]
  542.         except KeyError:
  543.             if default is self._MutableMapping__marker:
  544.                 raise 
  545.             default is self._MutableMapping__marker
  546.             return default
  547.  
  548.         del self[key]
  549.         return value
  550.  
  551.     
  552.     def popitem(self):
  553.         
  554.         try:
  555.             key = next(iter(self))
  556.         except StopIteration:
  557.             raise KeyError
  558.  
  559.         value = self[key]
  560.         del self[key]
  561.         return (key, value)
  562.  
  563.     
  564.     def clear(self):
  565.         
  566.         try:
  567.             while True:
  568.                 self.popitem()
  569.         except KeyError:
  570.             pass
  571.  
  572.  
  573.     
  574.     def update(self, other = (), **kwds):
  575.         if isinstance(other, Mapping):
  576.             for key in other:
  577.                 self[key] = other[key]
  578.             
  579.         elif hasattr(other, 'keys'):
  580.             for key in other.keys():
  581.                 self[key] = other[key]
  582.             
  583.         else:
  584.             for key, value in other:
  585.                 self[key] = value
  586.             
  587.         for key, value in kwds.items():
  588.             self[key] = value
  589.         
  590.  
  591.     
  592.     def setdefault(self, key, default = None):
  593.         
  594.         try:
  595.             return self[key]
  596.         except KeyError:
  597.             self[key] = default
  598.  
  599.         return default
  600.  
  601.  
  602. MutableMapping.register(dict)
  603.  
  604. class Sequence(Sized, Iterable, Container):
  605.     '''All the operations on a read-only sequence.
  606.  
  607.     Concrete subclasses must override __new__ or __init__,
  608.     __getitem__, and __len__.
  609.     '''
  610.     
  611.     def __getitem__(self, index):
  612.         raise IndexError
  613.  
  614.     __getitem__ = abstractmethod(__getitem__)
  615.     
  616.     def __iter__(self):
  617.         i = 0
  618.         
  619.         try:
  620.             while True:
  621.                 v = self[i]
  622.                 yield v
  623.                 i += 1
  624.         except IndexError:
  625.             return None
  626.  
  627.  
  628.     
  629.     def __contains__(self, value):
  630.         for v in self:
  631.             if v == value:
  632.                 return True
  633.         
  634.         return False
  635.  
  636.     
  637.     def __reversed__(self):
  638.         for i in reversed(range(len(self))):
  639.             yield self[i]
  640.         
  641.  
  642.     
  643.     def index(self, value):
  644.         for i, v in enumerate(self):
  645.             if v == value:
  646.                 return i
  647.         
  648.         raise ValueError
  649.  
  650.     
  651.     def count(self, value):
  652.         return (sum,)((lambda .0: for v in .0:
  653. if v == value:
  654. 1continue)(self))
  655.  
  656.  
  657. Sequence.register(tuple)
  658. Sequence.register(basestring)
  659. Sequence.register(buffer)
  660. Sequence.register(xrange)
  661.  
  662. class MutableSequence(Sequence):
  663.     
  664.     def __setitem__(self, index, value):
  665.         raise IndexError
  666.  
  667.     __setitem__ = abstractmethod(__setitem__)
  668.     
  669.     def __delitem__(self, index):
  670.         raise IndexError
  671.  
  672.     __delitem__ = abstractmethod(__delitem__)
  673.     
  674.     def insert(self, index, value):
  675.         raise IndexError
  676.  
  677.     insert = abstractmethod(insert)
  678.     
  679.     def append(self, value):
  680.         self.insert(len(self), value)
  681.  
  682.     
  683.     def reverse(self):
  684.         n = len(self)
  685.         for i in range(n // 2):
  686.             self[i] = self[n - i - 1]
  687.             self[n - i - 1] = self[i]
  688.         
  689.  
  690.     
  691.     def extend(self, values):
  692.         for v in values:
  693.             self.append(v)
  694.         
  695.  
  696.     
  697.     def pop(self, index = -1):
  698.         v = self[index]
  699.         del self[index]
  700.         return v
  701.  
  702.     
  703.     def remove(self, value):
  704.         del self[self.index(value)]
  705.  
  706.     
  707.     def __iadd__(self, values):
  708.         self.extend(values)
  709.         return self
  710.  
  711.  
  712. MutableSequence.register(list)
  713.